package com.ledim.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import com.ledim.adapter.base.LedimBaseListAdapter;
import com.ledim.bean.LedimEpisodeBean;
import com.ledim.utils.DensityUtil;
import com.letv.android.young.client.R;
import java.util.ArrayList;

public class FiguresAdapter extends LedimBaseListAdapter<LedimEpisodeBean> {

    private static final int POSITIVE = 180001;
    private static final int TV = 181031;
    private int selectedPosition = -1;
    private int mWidth;

    public FiguresAdapter(Context context, ArrayList<LedimEpisodeBean> episodes) {
        super(context);
        setDatas(episodes);
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        mWidth = wm.getDefaultDisplay().getWidth();
        int mHeight = wm.getDefaultDisplay().getHeight();
        if (mWidth > mHeight) {
            mWidth = mHeight;
        }
        mWidth = (int) ((mWidth - DensityUtil.dip2px(context, 48)) / 5.5);
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_episodes_listview, null);
            holder.mBg = convertView.findViewById(R.id.item_episode_bg);
            holder.mChoose = (ImageView) convertView.findViewById(R.id.item_episode_choose);
            holder.mNum = (TextView) convertView.findViewById(R.id.item_episode_num);
            ViewGroup.LayoutParams params = holder.mBg.getLayoutParams();
            params.height = mWidth;
            params.width = mWidth;
            holder.mBg.setLayoutParams(params);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.mBg.setVisibility(View.GONE);
        final LedimEpisodeBean episode = getItem(position);
        if (episode.videoType != null && !episode.videoType.equals("{}")) {
            switch (episode.getVedioType()) {
                case POSITIVE://正片
                case TV:
                    holder.mBg.setVisibility(View.VISIBLE);
                    holder.mNum.setText(episode.episode);
                    break;
                default:
                    break;
            }
        }
        if (selectedPosition == position) {
            holder.mNum.setTextColor(Color.parseColor("#1a1a1a"));
            holder.mBg.setBackgroundResource(R.drawable.yellow_box1);
            holder.mChoose.setVisibility(View.VISIBLE);
        } else {
            holder.mNum.setTextColor(Color.parseColor("#666666"));
            holder.mBg.setBackgroundResource(R.drawable.gray_box1);
            holder.mChoose.setVisibility(View.GONE);
        }
        return convertView;
    }

    public static class ViewHolder {

        private TextView mNum;
        private View mBg;
        private ImageView mChoose;
    }

    //listview选中的item
    public void setSelectItem(int position) {
        this.selectedPosition = position;
    }
}
